﻿using System.Collections.Generic;
using DMS.BEs;
using DMS.Filters;
using NHibernate;
using NHibernate.Criterion;

namespace DMS.DAOs.NH
{
    public class InvoiceDAO : GenericDAO<Invoice>, IInvoiceDAO
    {
        public InvoiceDAO(ISessionFactory sessionFactory)
            : base(sessionFactory)
        { }

        public IList<Invoice> GetByFilter(InvoiceFilter filter)
        {
            ICriteria query = Session.CreateCriteria<Invoice>();

            if (!string.IsNullOrEmpty(filter.Country))
                query.Add(Expression.Eq("BillingCountry", filter.Country));
            if (filter.CustomerId != 0)
                query.Add(Expression.Eq("Customer.Id", filter.CustomerId));
			if (filter.From.HasValue)
				query.Add(Expression.Gt("InvoiceDate", filter.From.Value));
			if (filter.To.HasValue)
				query.Add(Expression.Lt("InvoiceDate", filter.To.Value));

            return query.List<Invoice>();
        }
    }
}